package other.edan.erp.k3;

import com.rkhd.platform.sdk.data.model.Product;
import com.rkhd.platform.sdk.data.model.ShippingPersonnelSheet__c;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 新增、编辑【出货/装机相关人员】触发计算
 * @Author: 黎炎松
 * @Date: 2024/12/11 16:20
 */
public class CalcFieldTrigger implements Trigger {

    private static final Logger LOGGER = LoggerFactory.getLogger();

    /**
     * trigger 触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        LOGGER.debug("出货/装机相关人员 触发开始");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<ShippingPersonnelSheet__c> sheetList = new ArrayList<>();
        for (XObject xObject : dataList) {
            sheetList.add((ShippingPersonnelSheet__c) xObject);
            dataResultList.add(new DataResult(true, "", xObject));
        }

        // 判断是处理出库还是装机
        ShippingPersonnelSheet__c sheet = sheetList.get(0);
        Long entityType = sheet.getEntityType();
        if (entityType == 3590014698429035L) {
            LOGGER.debug("计算装机模块");
            CalcInstallInfoField calcInstallInfoField = new CalcInstallInfoField();
            calcInstallInfoField.dealInstallInfoField(sheet.getInstallationInfo__c());
        } else {
            LOGGER.debug("计算出库模块");
            CalcSalesOutField calcSalesOutField = new CalcSalesOutField();
            calcSalesOutField.dealSalesOutField(sheet.getSalesOutReceipt__c());
        }

        LOGGER.debug("出货/装机相关人员 处理完成");
        return new TriggerResponse(true, "处理完成", dataResultList);
    }



}
